<?php 
 require_once(dirname(__FILE__).'/../include/config.inc.php');
require_once "function.php";

//工单提交处理
$post_time		=time();
$post_time2		=date("Y-m-d H:i:s",time());
 
 
$h=date("H",time());
$h=intval($h);


//========= 下班执行  ============
// 列出系统中 签到后 未处理的数据

if($cfg_xia=="Y"){
	$beginToday=mktime(18,30,0,date('m'),date('d'),date('Y'));  //下班开始时间
}
if($cfg_xia=="N"){
	$beginToday=mktime(18,00,0,date('m'),date('d'),date('Y'));  //下班开始时间
}


$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1; //今天的结束时间
if($post_time>$beginToday && $post_time<$endToday	){
	echo "下班执行开始";
	
	$sql_order_2 = "SELECT id,manageid,post_time,repairid  FROM  `#@__order`  where    jindu='4'   and  delstate=''  and qiandao='true'  order by id desc   ";
	$dosql->Execute($sql_order_2);
	while($row2 = $dosql->GetArray())
	{
		$orderid=$row2['id'];
		$mids=$row2['manageid'];
		$orderidname=date('Ymd',$row2['post_time'])."-".$orderid;  //工单号
		$repairname=getRepairMode($row2['repairid']);  //任务名称
		$apply_time=date("Y-m-d H:i:s",$row2['post_time']);
	
		//查找 任务日志
		$row_order=$dosql->GetOne("SELECT *  FROM  `#@__order_log`  where   orderid='$orderid'   AND jindu='4'   order by id desc   ");
		if( is_array($row_order) && $row_order['qiandao']=='true' ){
		$last_time=$row_order['op_time'];
	
		//执行人
		$zhixingren="";
		$ids_arr=explode(",",$mids);
		foreach($ids_arr as $key=>$val){
			$zhixingren.=getManage($val)."    ";
		}
		
		//消息推送
			//判断是否 推送消息
			$row_timeout=$dosql->GetOne("SELECT *  FROM  `#@__timeout_log`  where   orderid='$orderid'   AND  jindu='4' AND sendmsg='true'  AND qiandao='1' order by id desc   ");
			if(is_array($row_timeout) && isset($row_timeout['id']) ){
					$last_outtime=$row_timeout['op_time'];
					// 判断是否  再次 超过 
					if($post_time-$last_outtime>1800){
						//再次超时 ，记录入库 并发送消息
						foreach($ids_arr as $key=>$val){
							//获取工程师 openid
							$m=$dosql->GetOne("select openid  from `#@__user`  where id='$val'");
							$openid_str	  =$m['openid'];
							//插入更新的状态
						$dosql->ExecNoneQuery("insert into `#@__timeout_log`(orderid,op,op_time,jindu,manageid,sendmsg,qiandao) values ('$orderid','下班签到工单未处理，再次提醒','$post_time','4','$val','true','1')");
						
						$json2='{
							  "touser": "'.$openid_str.'" ,  
							  "template_id": "'.$cfg_order_status.'",    
							  "url":"'.$cfg_weburl.'wx/morderDetail.php?orderid='.$orderid.'",  
							  "data": {
								  "first": {
									  "value": "您好，下班了，你有工单还没有完成或反馈。", 
									  "color": "#f71b03"
								  }, 
								  "keyword1": {
									  "value": "'.$orderidname.'", 
									  "color": "#173177"
								  }, 
								  "keyword2": {
									  "value": "'.$apply_time.'", 
									  "color": "#173177"
								  },
								  "keyword3": {
									  "value": "'.$zhixingren.'", 
									  "color": "#0e48cb"
								  },
								  "keyword4": {
									  "value": " '.$repairname.'", 
									  "color": "#173177"
								  },
								  "keyword5": {
									  "value": "如任务结束请进行完修申报，如未结束请及时反馈。", 
									  "color": "#173177"
								  },
								  "remark": {
									  "value": "请点击处理。如90分钟后未处理，系统将再次扣除奖励1元。服务热线：'.$cfg_hotline.'", 
									  "color": "#f71b03"
								  } 
							  }
							}';
						sendWxMsg($json2);
						
						
						}	
					}
			}else{
				//首次超时 ，记录入库 并发送消息
				
				foreach($ids_arr as $key=>$val){
					//获取工程师 openid
					$m=$dosql->GetOne("select openid  from `#@__user`  where id='$val'");
					$openid_str	  =$m['openid'];
					//插入更新的状态
					$dosql->ExecNoneQuery("insert into `#@__timeout_log`(orderid,op,op_time,jindu,manageid,sendmsg,qiandao) values ('$orderid','下班后签到工单未处理，首次提示','$post_time','4','$val','true','1')");
						$json2='{
							  "touser": "'.$openid_str.'" ,  
							  "template_id": "'.$cfg_order_status.'",    
							  "url":"'.$cfg_weburl.'wx/morderDetail.php?orderid='.$orderid.'",  
							  "data": {
								  "first": {
									  "value": "您好，下班了，你有工单还没有完成或反馈。", 
									  "color": "#f71b03"
								  }, 
								  "keyword1": {
									  "value": "'.$orderidname.'", 
									  "color": "#173177"
								  }, 
								  "keyword2": {
									  "value": "'.$apply_time.'", 
									  "color": "#173177"
								  },
								  "keyword3": {
									  "value": "'.$zhixingren.'", 
									  "color": "#0e48cb"
								  },
								  "keyword4": {
									  "value": " '.$repairname.'", 
									  "color": "#173177"
								  },
								  "keyword5": {
									  "value": "如任务结束请进行完修申报，如未结束请及时反馈。", 
									  "color": "#173177"
								  },
								  "remark": {
									   "value": "请点击处理，及时接单。如90分钟后不处理，系统将扣除奖励1元。服务热线：'.$cfg_hotline.'", 
									  "color": "#f71b03"
								  } 
							  }
							}';
						sendWxMsg($json2);
						
				}	
			}
	
		
		// 扣除奖励 
		if($post_time-$last_time>1900){
			//是否扣费
			$row_fee=$dosql->GetOne("SELECT *  FROM  `#@__fee_log`  where   orderid='$orderid'   AND jindu='4'  AND op='move' and qiandao='1'  order by id desc   ");
			if(is_array($row_fee) && isset($row_fee['id']) ){
					$last_feetime=$row_fee['op_time'];
					// 判断是否  再次 超过 
					if($post_time-$last_feetime>7200){
						//再次超时 ，记录入库 并发送消息
						foreach($ids_arr as $key=>$val){
							//获取工程师 openid
							$m=$dosql->GetOne("select openid  from `#@__user`  where id='$val'");
							$openid_str	  =$m['openid'];
							//插入更新的状态
						$dosql->ExecNoneQuery("insert into `#@__fee_log`(orderid,op,op_time,jindu,manageid,fee,remark,qiandao) values ('$orderid','move','$post_time','3','$val','1','工单2小时没有处理，再次扣除奖励1元','1')");
						 //推送超时提醒
	
						}	
					}
			}else{
				//首次超时 ，记录入库 并发送消息
				foreach($ids_arr as $key=>$val){
					//获取工程师 openid
					$m=$dosql->GetOne("select openid  from `#@__user`  where id='$val'");
					$openid_str	  =$m['openid'];
					//插入更新的状态
				$dosql->ExecNoneQuery("insert into `#@__fee_log`(orderid,op,op_time,jindu,manageid,fee,remark,qiandao) values ('$orderid','move','$post_time','3','$val','1','工单2小时没有处理，扣除奖励1元','1')");
				}	
			}
	
		}
		
	}
	}
}else{
	echo "没有执行下班";
}



?>